﻿using System;
using System.Collections.Generic;

namespace DesignPattern.test {
    public class MyArray {
        private int len = 8;

        int[,] arr = new int[8, 8]{
            //0  1  2  3  4  5  6  7
            {0, 0, 0, 0, 0, 0, 0, 0}, //0
            {0, 0, 0, 0, 0, 0, 0, 0}, //1
            {0, 0, 1, 0, 0, 0, 0, 0}, //2
            {0, 0, 0, 0, 0, 0, 0, 0}, //3
            {0, 0, 0, 0, 0, 0, 0, 0}, //4
            {0, 0, 0, 0, 0, 0, 0, 0}, //5
            {0, 0, 0, 0, 0, 0, 0, 0}, //6
            {0, 0, 0, 0, 0, 0, 0, 0}, //7
        };

        private struct Point {
            private int _x;
            private int _y;

            public int X {
                get { return _x; }
                set { _x = value; }
            }

            public int Y {
                get { return _y; }
                set { _y = value; }
            }

            public Point(int x, int y){
                _x = x;
                _y = y;
            }

            public override string ToString(){
                return string.Format("({0},{1})", _x, _y);
            }
        }

        private struct Line {
            private Point start;
            private Point end;

            public Line(Point start, Point end){
                this.start = start;
                this.end = end;
            }

            public Point Start {
                get { return start; }
                set { start = value; }
            }

            public Point End {
                get { return end; }
                set { end = value; }
            }


            public override string ToString(){
                return string.Format("[start:{0},end:{1}]", start, end);
            }
        }

        public void test(){
//            process();
            eq();
        }

        private void eq(){
            Point p0 = new Point(1, 2);
            Point p1 = new Point(1, 2);
            Console.WriteLine(p0.GetHashCode());
            Console.WriteLine(p1.GetHashCode());
            Console.WriteLine(p0.Equals(p0));
            Console.WriteLine(p0.Equals(p1));

            Line line0 = new Line(p0, p1);
            Line line1 = new Line(p1, p0);
            Console.WriteLine(line0.GetHashCode());
            Console.WriteLine(line1.GetHashCode());
            Console.WriteLine(line0.Equals(line1));
        }

        private void process(){
            Point startPRow = new Point(-99, -99);
            Point endPRow = new Point(-99, -99);

            Point startPCol = new Point(-99, -99);
            Point endPCol = new Point(-99, -99);
            HashSet<Line> lineSet = new HashSet<Line>();

            List<Line> lineList = new List<Line>();
            //是否是第一个
            bool firstFlagRow = true;
            bool firstFlagCol = true;
            for (int i = 0; i < len; i++){
                for (int j = 0; j < len; j++){
                    if (arr[i, j] == 1 && firstFlagRow){
                        startPRow = new Point(i, j);
                        firstFlagRow = false;
                        if (j == len - 1){
                            firstFlagRow = true;
                            endPRow = new Point(i, j);
                            if (!startPRow.Equals(endPRow)){
                                lineList.Add(new Line(startPRow, endPRow));
                                lineSet.Add(new Line(startPRow, endPRow));    
                            }
                        }
                    }
                    else if (arr[i, j] == 0 && !firstFlagRow){
                        firstFlagRow = true;
                        endPRow = new Point(i, j - 1);
                        if (!startPRow.Equals(endPRow)){
                            lineList.Add(new Line(startPRow, endPRow));
                            lineSet.Add(new Line(startPRow, endPRow));    
                        }
                    }
                    else if (j == len - 1 && arr[i, j] == 1 && !firstFlagRow){
                        firstFlagRow = true;
                        endPRow = new Point(i, j);
                        if (!startPRow.Equals(endPRow)){
                            lineList.Add(new Line(startPRow, endPRow));
                            lineSet.Add(new Line(startPRow, endPRow));    
                        }
                    }

                    if (arr[j, i] == 1 && firstFlagCol){
                        startPCol = new Point(j, i);
                        firstFlagCol = false;
                        if (j == len - 1){
                            firstFlagCol = true;
                            endPCol = new Point(j, i);
                            if (!startPCol.Equals(endPCol)){
                                lineList.Add(new Line(startPCol, endPCol));
                                lineSet.Add(new Line(startPCol, endPCol));
                            }
                            
                        }
                    }
                    else if (arr[j, i] == 0 && !firstFlagCol){
                        firstFlagCol = true;
                        endPCol = new Point(j - 1, i);
                        if (!startPCol.Equals(endPCol)){
                            lineList.Add(new Line(startPCol, endPCol));
                            lineSet.Add(new Line(startPCol, endPCol));
                        }
                    }
                    else if (j == len - 1 && arr[j, i] == 1 && !firstFlagCol){
                        firstFlagCol = true;
                        endPCol = new Point(j , i);
                        if (!startPCol.Equals(endPCol)){
                            lineList.Add(new Line(startPCol, endPCol));
                            lineSet.Add(new Line(startPCol, endPCol));
                        }
                    }
                }
            }

            foreach (var line in lineSet){
                Console.WriteLine(line);
            }

            for (int i = 0; i < lineList.Count; i++){
                Console.WriteLine(i + "," + lineList[i]);
            }
        }
    }
}